package com.webull.information.center.boot;

import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.type.AnnotatedTypeMetadata;

import com.webull.information.center.boot.CarwlerStarter.NewsJobsCondition;

@Configuration
@Conditional(NewsJobsCondition.class)
@ImportResource(locations = { "classpath:/webull-information-center/spring/carwler-app.xml" })
public class CarwlerStarter {

	@Conditional(NewsJobsCondition2.class)
	@ImportResource(locations = { 
			"classpath:/webull-information-center/spring/carwler-elasticJob.xml" })
	public class WebullElasticJob {
	}


    public static class NewsJobsCondition implements Condition {
        protected final Logger logger = LogManager.getLogger(getClass());

        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            boolean res = false;
            try {
				Properties appProperties = EnvProp.getEnvProp();
                String run = appProperties.getProperty("news.job.run");
                logger.info("CarwlerStarter.NewsJobsCondition.matches run:{}.!!!!!!!!!!!!!!!!!!!", run);
				res = "cn".equalsIgnoreCase(run);
            } catch (Exception e) {
                logger.error("CarwlerStarter.NewsJobsCondition.matches error.!!!!!!!!!!!!!!!!!!!", e);
            }
            logger.info("CarwlerStarter.NewsJobsCondition.matches result:{}.!!!!!!!!!!!!!!!!!!!", res);
            return res;
        }

    }

	public static class NewsJobsCondition2 implements Condition {
		protected final Logger logger = LogManager.getLogger(getClass());

		@Override
		public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
			boolean res = false;
			try {
				Properties appProperties = EnvProp.getEnvProp();
				String run = appProperties.getProperty("news.job.run");
				String run2 = appProperties.getProperty("news.job.run2", "true");
				logger.info("CarwlerStarter.NewsJobsCondition.matches run:{}.!!!!!!!!!!!!!!!!!!!", run);
				res = "cn".equalsIgnoreCase(run) && "true".equals(run2);
			} catch (Exception e) {
				logger.error("CarwlerStarter.NewsJobsCondition.matches error.!!!!!!!!!!!!!!!!!!!", e);
			}
			logger.info("CarwlerStarter.NewsJobsCondition.matches result:{}.!!!!!!!!!!!!!!!!!!!", res);
			return res;
		}

	}

}
